<template>
  <!-- 请购管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
        <el-button type="primary" @click="createPurchaseOrder"
          >生成采购单</el-button
        >
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="标题"
            clearable
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @selection-change="handleSelection"
        @row-dblclick="handleRowDblclick"
      >
        <el-table-column
          type="selection"
          width="55"
          :selectable="checkSelectable"
        >
        </el-table-column>
        <el-table-column label="ID" prop="id" width="50"></el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column
          label="请购部门"
          prop="departmentName"
          width="200"
        ></el-table-column>
        <el-table-column
          label="请购人"
          prop="adminName"
          width="200"
        ></el-table-column>
        <el-table-column
          label="经办人"
          prop="operatorName"
          width="200"
        ></el-table-column>
        <el-table-column label="请购金额" prop="productPrice" width="200">
          <template #default="scope">
            <p>{{ scope.row.productPrice }}元</p>
          </template>
        </el-table-column>
        <!-- <el-table-column label="请购物品" min-width="200">
          <template #default="scope">
            <p v-for="item in scope.row.requisitionProductList" :key="item.id">
              {{ item.productName }}：{{ item.specificationName
              }}{{ item.productBuyNum }}{{ item.unitName }}
            </p>
          </template>
        </el-table-column> -->
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="审核状态" prop="status" width="150" sortable>
          <template #default="scope">
            <!--0，未审核，1,审核通过，2审核未通过-->
            <p v-if="scope.row.status == 0">待审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3" style="color: #ff9b31">审核中</p>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="requisitionState" width="200">
          <template #default="scope">
            <!--请购单状态：0未生单 1部分生单 2已生单-->
            <p v-if="scope.row.requisitionState == 0">未生单</p>
            <p v-if="scope.row.requisitionState == 1">部分生单</p>
            <p v-if="scope.row.requisitionState == 2">已生单</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-if="scope.row.status == 0"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定作废吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-auth-button="['scrap/purchaseRequest']"
                v-if="scope.row.status != 0"
              >
                <template #reference>
                  <el-button text type="primary" size="small">作废</el-button>
                </template>
              </el-popconfirm>
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-if="scope.row.status == 0"
              >
                <template #reference>
                  <el-button text type="primary" size="small">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>
<script>
export default {
  name: "purchaseRequest",
  data () {
    return {
      apiObj: this.$API.purchase.getPurchaseRequestList,
      searchValue: {
        search: null,
      },
      selection: [],
    }
  },

  methods: {
    //添加
    add () {
      this.$router.push({
        path: "/goods/purchaseRequest/addPurchaseRequest",
        query: {
          mode: "add",
        },
      })
    },

    // 生成采购单
    createPurchaseOrder () {
      console.log(this.selection)
      let state_flag = false
      this.selection.map((item) => {
        if (item.state == 0) {
          state_flag = true
          this.$message.error(
            `所选存在未审批订单，不可以进行生成订单操作`
          )
        }
      })
      if (state_flag) return

      if (this.selection.length == 0) {
        this.$message.error(`该选择需要生成采购单的请购单`)
        return
      }
      let ids = this.selection.map((item) => item.id)
      this.$router.push({
        path: "/goods/purchaseRequest/createPurchaseOrder",
        query: {
          ids: ids.join(","),
        },
      })
    },

    //编辑
    table_edit (row) {
      this.$router.push({
        path: "/goods/purchaseRequest/addPurchaseRequest",
        query: {
          mode: "edit",
          id: row.id,
        },
      })
    },
    //查看
    table_show (row) {
      this.$router.push({
        path: "/goods/purchaseRequest/purchaseRequestDetail",
        query: {
          id: row.id,
        },
      })
    },
    //删除
    async table_del (row) {
      let data = {
        id: row.id,
      }
      var res = await this.$API.purchase.deletePurchaseRequest.post(data)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      }
    },

    // 选择请购单
    handleSelection (list) {
      this.selection = list
    },

    // 判断请购单是否可被选择
    checkSelectable (row) {
      return row.requisitionState != 2 && row.status == 1
    },

    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style></style>
